define(["angularAMD", 'moment'], function (angularAMD, moment) {
    angularAMD.service('$MaterialNeed', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog){
            var service = this;

            service.init = function(options){
                console.log('options.info_list',options.info_list);
                (function openDialog(){
                    $mdDialog.dialog(
                        'module/moduleSMT/program/SMT17/materialNeed.tmp.html',
                        function (dialog) {
                            return {
                                title:$filter('translate')('SMT17.material_need_dialog.title'),
                                plot_no:'',
                                eqp_no:'',
                                info_list:options.info_list,
                                picklist_type:'1',
                                material_station:'',
                                pos_station:{//設備站位
                                    slot_no:'',
                                    material_no:'',
                                    qty:''
                                },
                                produce_ver:[],
                                product_qty:1,
                                material_no:'',
                                item_list:[],
                                material_open:function(){
                                    if(dialog.picklist_type =='2'){
                                    }else{
                                        $rootScope.showQueryList({
                                            pageRecord: 50,
                                            header: [
                                                { label: $filter('translate')('SMT17.material_need_dialog.material_station.column1'), code: 'slot_no' },
                                                { label: $filter('translate')('SMT17.material_need_dialog.material_station.column2'), code: 'material_no' },
                                                { label: $filter('translate')('SMT17.material_need_dialog.material_station.column3'), code: 'smaterial_no' },
                                                { label: $filter('translate')('SMT17.material_need_dialog.material_station.column4'), code: 'qty' }
                                            ],
                                            query: function (conditions, feedback) {
                                                $MMWService.sendToServer({
                                                    uri: 'ServicesSMT.Module_SMT.mtl_request_info_get',
                                                    content: {
                                                        eqp_no:options.info_list.machine_no,
                                                        feederlist_no:options.info_list.feederlist_no,
                                                        feederlist_ver:options.info_list.feederlist_ver,
                                                        panel_side:options.info_list.panel_side,
                                                    },
                                                    success: function (data) {
                                                        feedback(data.feeder_list);
                                                    }
                                                });
                                            },
                                            selectCode: dialog.pos_station.material_station,
                                            confirm: function (selectItem, qy_dialog) {
                                                dialog.pos_station.slot_no = selectItem.slot_no;
                                                dialog.pos_station.material_no = selectItem.material_no;
                                                dialog.pos_station.qty = selectItem.qty;
                                                qy_dialog.hide();
                                            }
                                        });
                                    }
                                },
                                back:function(){
                                    dialog.hide();
                                },
                                chooseNeedList:function(info){
                                    dialog.picklist_type = info;
                                    dialog.pos_station = {};
                                    dialog.produce_ver = [];
                                    dialog.item_list = [];
                                    if(info === '2'){
                                        dialog.send_product_ver();
                                    }
                                },
                                send_product_ver:function(){
                                    $MMWService.sendToServer({
                                        uri: 'ServicesSMT.Module_SMT.mtl_request_info_get',
                                        content: {
                                            eqp_no:options.info_list.machine_no,
                                            feederlist_no:options.info_list.feederlist_no,
                                            feederlist_ver:options.info_list.feederlist_ver,
                                            panel_side:options.info_list.panel_side,
                                        },
                                        success: function (data) {
                                            dialog.produce_ver = data.feeder_list;
                                        }
                                    });
                                },
                                confirm: function () {
                                    if(dialog.picklist_type == '1'){
                                        if(dialog.pos_station.slot_no == ''){
                                            $rootScope.showAlert($filter('translate')('SMT17.msg.input_no_data'));
                                            return;
                                        }
                                        let info = {
                                            slot_no:dialog.pos_station.slot_no,
                                            material_no:dialog.pos_station.material_no,
                                            demand_qty:'1',
                                            qty:'1'
                                        };
                                        dialog.item_list.unshift(info);
                                    }else if(dialog.picklist_type == '2'){
                                        dialog.item_list = [];
                                        dialog.produce_ver.forEach(function(item){
                                            item.demand_qty = item.qty * dialog.product_qty;
                                            dialog.item_list.unshift({
                                                slot_no:item.slot_no,
                                                material_no:item.material_no,
                                                demand_qty:item.demand_qty,
                                                qty:item.demand_qty
                                            });
                                        })
                                    }
                                    // dialog.produce_ver = [];
                                    dialog.pos_station = {//設備站位
                                        slot_no:'',
                                        material_no:'',
                                        qty:''
                                    }
                                },
                                execute:function(){
                                    let content = {
                                        picklist_type:dialog.picklist_type,
                                        op_no:options.info_list.op_no,
                                        eqp_no:options.info_list.machine_no,
                                        feederlist_no:options.info_list.feederlist_no,
                                        feederlist_ver:options.info_list.feederlist_ver,
                                        mo_no:options.info_list.work_no,
                                        feeder_list:dialog.item_list
                                    }
                                    $MMWService.sendToServer({
                                        uri: 'ServicesSMT.Module_SMT.mtl_request_process',
                                        content,
                                        success: function (data) {
                                            $rootScope.showToast($filter('translate')('common.msg.success'));
                                            dialog.item_list = [];
                                        }
                                    });
                                }
                            }
                        }
                    );
                })();
            };
        }
    ]);
});
